
/*


Table B19 and B23


*/




local nametable = "tables/rev_" 



forvalues xd=1(1)1{
	
	if `xd'==1{
		local xdist = 50
	}




	foreach xcovariate in "distcity" "pop00" {
	


	*-----
	eststo clear
	local column_number = 1
	local column_name = "column" + "`column_number'"
	*---
	

*Land use

	
	use "public_data/g30.dta", clear
	
	drop if adist>`xdist'
	local   nweight 			= "No"
	
	drop if careawall==.
	
	gen interdep = careawall>0
	replace weight = 1
	
		

		*computes mean of dependent var
		egen interm = mean(interdep)
		local vmeandep = interm[1]
		local meandep = string(`vmeandep')
		if index("`meandep'",".")==1{
			local meandep = "0"+substr("`meandep'",1,3)
		}
		if index("`meandep'",".")<5&index("`meandep'",".")>1{
			local meandep = substr("`meandep'",1,4)
		}
	

		*computes the number of country pairs
		egen intercp = group(border)
		egen intercpmax = max(intercp)
		local countrypair = intercpmax[1]
		drop intercp*			
	

	
*	xi: areg interdep  `xcovariate' varofi  [aw=weight] ,a(groupfe) cluster(border)
	reghdfe interdep `xcovariate' varofi [aw=weight], absorb(groupfe) vce(cluster groupfe country)
	eststo `column_name'
	estadd local space  					" "
	estadd local groupfe 					"No"
	estadd local countrypair "`countrypair'"
	estadd local polynomial				"No"
	estadd local maxdist 					"`xdist'"
	estadd local weight "`nweight'"
	estadd local nmeandep				"`meandep'"
	local column_number = `column_number' + 1
	local column_name = "column" + "`column_number'"	
	
	
	
*	xi: areg interdep  `xcovariate' rdist1*  varofi [aw=weight] ,a(groupfe) cluster(border)
	reghdfe interdep `xcovariate' rdist1*  varofi [aw=weight], absorb(groupfe) vce(cluster groupfe country)
	eststo `column_name'
	estadd local space  					" "
	estadd local groupfe 					"No"
	estadd local countrypair "`countrypair'"
	estadd local polynomial				"Linear"
	estadd local maxdist 					"`xdist'"
	estadd local weight "`nweight'"
	estadd local nmeandep				"`meandep'"
	local column_number = `column_number' + 1
	local column_name = "column" + "`column_number'"
	
	
*High vs low risk
 
foreach varg in "lowg" "higg"{

		use "public_data/g30.dta", clear
		
		drop if adist>`xdist'		
		local   nweight 			= "No"

		
		capture rename other_areat otherarea 
		capture rename fodder_areat fodderarea
		gen hign 	= 0
		gen lown 	= 1
				
		foreach crop_areat of varlist *_areat{
			local border_areat = "border_areat"
			qui bys groupfe: egen border_areat = max(`crop_areat')
			local crop 	= subinstr("`crop_areat'","_areat","",.)
			if "`crop'"=="cassavayam"{
				local crop = "cassava"
			}
			if "`crop'"== "cocoacoffeetea"{
				local crop = "cocoa"
			}
			if "`crop'"== "wspotato"{
				local crop = "spotato"
			}
			if "`crop'"== "bancocon"{
			local crop = "coconut"
			}
			if "`crop'"== "vegetables"{
				local crop = "tomato"
			}
			if "`crop'"== "pulses"{
				local crop = "phaseolus"
			}
			if "`crop'"== "cerealoth"{
				local crop = "barley"
			}	
			local crop_null 				= "`crop'" + "_null"
			
			
			capture drop inter*
			gen intermed = `crop_null'
			qui replace hign  = intermed 								if intermed>=hign&`border_areat'>0
			qui replace lown  = intermed 								if intermed<=lown&`border_areat'>0
			
			capture drop inter*
			drop border_areat
		}


		gen higa = 0
		gen lowa = 0
		foreach crop_areat of varlist *_areat{
		local crop 							= subinstr("`crop_areat'","_areat","",.)
		if "`crop'"=="cassavayam"{
			local crop = "cassava"
		}
		if "`crop'"== "cocoacoffeetea"{
			local crop = "cocoa"
		}
		if "`crop'"== "wspotato"{
			local crop = "spotato"
		}
		if "`crop'"== "bancocon"{
		local crop = "coconut"
		}
		if "`crop'"== "vegetables"{
			local crop = "tomato"
		}
		if "`crop'"== "pulses"{
			local crop = "phaseolus"
		}
		if "`crop'"== "cerealoth"{
			local crop = "barley"
		}
		local crop_null = "`crop'"+"_null"
		qui replace higa  = higa+`crop_areat' if `crop_null'==hign&`crop_areat'<.
		qui replace lowa  = lowa+`crop_areat' if `crop_null'==lown&`crop_areat'<.
		}
*
		gen lowg = lowa>0
		gen higg = higa>0


		replace weight = 1
		gen interdep = `varg'


		*computes mean of dependent var
		egen interm = mean(interdep)
		local vmeandep = interm[1]
		local meandep = string(`vmeandep')
		if index("`meandep'",".")==1{
			local meandep = "0"+substr("`meandep'",1,3)
		}
		if index("`meandep'",".")<5&index("`meandep'",".")>1{
			local meandep = substr("`meandep'",1,4)
		}
	

		*computes the number of country pairs
		egen intercp = group(border)
		egen intercpmax = max(intercp)
		local countrypair = intercpmax[1]
		drop intercp*			
	
	
*		xi: areg interdep  `xcovariate'  varofi   [aw=weight],a(groupfe) cluster(border)
		reghdfe interdep `xcovariate' varofi [aw=weight], absorb(groupfe) vce(cluster groupfe country)
		eststo `column_name'
		estadd local space  					" "
		estadd local groupfe 					"Yes"
		estadd local countrypair "`countrypair'"
		estadd local maxdist 					"`xdist'"
		estadd local weight "`nweight'"
		estadd local polynomial				"No"
		estadd local nmeandep				"`meandep'"
		local column_number = `column_number' + 1
		local column_name = "column" + "`column_number'"
	

	
*		xi: areg interdep  `xcovariate' rdist1* varofi  [aw=weight] ,a(groupfe) cluster(border)
		reghdfe interdep `xcovariate' rdist1*  varofi [aw=weight], absorb(groupfe) vce(cluster groupfe country)
		eststo `column_name'
		estadd local space  					" "
		estadd local groupfe 					"Yes"
		estadd local countrypair "`countrypair'"
		estadd local maxdist 					"`xdist'"
		estadd local weight "`nweight'"
		estadd local polynomial				"Linear"
		estadd local nmeandep				"`meandep'"
		local column_number = `column_number' + 1
		local column_name = "column" + "`column_number'"	
		


}

	
  
 

*Crop diversification



	use "public_data/g30.dta", clear
	drop if adist>`xdist'
	
	gen interdep = ln(ncropswall/careawall)
	
	replace weight 			= careawall
	local   nweight 			= "Yes"
	
	
	egen interm = mean(interdep)
	local vmeandep = interm[1]
	local meandep = string(`vmeandep')
	if index("`meandep'",".")==1{
		local meandep = "0"+substr("`meandep'",1,3)
	}
	if index("`meandep'",".")<5&index("`meandep'",".")>1{
		local meandep = substr("`meandep'",1,4)
	}
	di "`meandep'"
	
	


	
*	xi: areg interdep  `xcovariate'  varofi  [aw=weight]  ,a(groupfe) cluster(border)
	reghdfe interdep `xcovariate' varofi [aw=weight], absorb(groupfe) vce(cluster groupfe country)
	eststo `column_name'
	estadd local polynomial "No"
	estadd local groupfe "Yes"
	estadd local distfe "No"
	estadd local countrypair "`countrypair'"
	estadd local weight "`nweight'"
	estadd local maxdist 					"`xdist'"
	estadd local nmeandep				"`meandep'"
	estadd local space  					" "
	local column_number = `column_number' + 1
	local column_name = "column" + "`column_number'"	
	

	
*	xi: areg interdep  `xcovariate' rdist1*  varofi  [aw=weight]  ,a(groupfe) cluster(border)
	reghdfe interdep `xcovariate' rdist1*  varofi [aw=weight], absorb(groupfe) vce(cluster groupfe country)
	eststo `column_name'
	estadd local polynomial "Linear"
	estadd local groupfe "Yes"
	estadd local distfe "No"
	estadd local countrypair "`countrypair'"
	estadd local weight "`nweight'"
	estadd local maxdist 					"`xdist'"
	estadd local nmeandep				"`meandep'"
	estadd local space  					" "
	local column_number = `column_number' + 1
	local column_name = "column" + "`column_number'"	
	

	
				
				


*	
label var interdep  " "		

label var distcity 	"Log Distance to Closest City "
label var pop00  	"Log population density "

local nametable1 = "`nametable'" + "`xcovariate'" + ".tex"

#delimit ;
esttab column1 column2 column3 column4 column5 column6 column7 column8
using "`nametable1'", replace
label
b(3)
se(3)
star(* 0.10 ** 0.05 *** 0.01)
width(1.0\hsize)
nonotes
nolines
noconstant
mgroups( " \textit{ Plot cultivated }"   "\textit{Lowest risk crop grown}" "\textit{Highest risk crop grown}" " \textit{ log \# crops by 10$ km^2 $ of cultivated land }" , pattern(1 0 1 0 1 0 1 0)
prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) 
keep(varofi  "`xcovariate'")
order(varofi "`xcovariate'")
 stats(groupfe					space	polynomial		space	r2		N					countrypair			space	weight		space	maxdist							space	nmeandep,
    fmt(0						0		0				0		3		0					0					0		0			0		0								0		0)
layout(@						@		@				@		@		@					@					@		@			@		@								@		@)	 	
labels("Border x Ethnic FE"		" "		"RD Polynomial"	" "		"R2"	"\# Observations"	"\# Country pairs"	" "		"Weights" 	" "		"Max distance to border (km)"	" " 	"Mean Dep. Var.")
 )	
noobs
booktabs
;
#delimit cr

}

}



